{% url 'forgetpassword-view' as forgetpassword_url %}
{% url 'register-view' as register_url %}
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>

<div id="web-auth-login" class="">   
    <div class="col-md-12 column">
			<div class="row clearfix">

				<div class="col-md-3 column">
                    </div>

            <div class="col-md-6 column">
    <div class="panel panel-web-auth">

        <div class="panel-heading">
            <h1 class="text-center" style="color:#f45353">请登陆</h1>
        </div>


        <form id="web-auth-login-form" class="clearfix" method="post" role="form">
            	<div class="col-md-3 column">
                    </div>
            <div class="col-md-6 column">
            {% csrf_token %}
            <div class="form-group">
                <div class="input-group">
                    <div class="input-group-addon">
                        <span class="glyphicon glyphicon-user"></span>
                    </div>
                    <input id="web-auth-login-username"type="text" class="form-control">
                </div>
            </div>

            <div class="form-group">
                <div class="input-group">
                    <div class="input-group-addon">
                        <span class="glyphicon glyphicon-lock"></span>
                    </div>
                    <input id="web-auth-login-password" type="password" class="form-control">
                </div>
            </div>
            <div id="web-auth-forgetpassword"><a href="{{forgetpassword_url}}">忘记密码?</a></div>
            <button id="web-auth-login-button" type="submit" class="btn btn-web-auth pull-left btn btn-danger">登录</button>
                <a href="{{register_url}}" type="button" class="btn btn-web-auth pull-right ">注册</a>
                </div>
            <div class="col-md-3 column">
                    </div>
                     
           
        </form>
    </div>
                </div>
                </div>

        <div class="col-md-3 column">
                    </div>
    </div>
</div>


<script language="javascript" type="text/javascript">
    $('#web-auth-login-form').submit(function(){
        $.ajax({
            type:"POST",
            url:"/usercontrol/login",
            data:{"username":$("#web-auth-login-username").val(),"password":$("#web-auth-login-password").val()},
            beforeSend:function(xhr){
                xhr.setRequestHeader("X-CSRFToken", $.cookie('csrftoken'));  
            },
            success:function(data,textStatus){
                var errors = data["errors"];
                if(errors.length==0){
                    location.replace("/"); 
                }
                else{
                    //alert(errors);
                    var str = ""
                    for (var key in errors){
                        str += errors[key]+"\n";
                    }
                    alert(str);
                }
                
            },
            error:function(XMLHttpRequest, textStatus, errorThrown){
                alert(XMLHttpRequest.responseText);
            }
        });
        return false;
    }); 
</script>
